/**
 * Author: w景洋.
 * Time: 2016/4/27 0027
 */
angular.module('usercenter.address', [])
    .config(['$stateProvider', '$urlRouterProvider',
        function ($stateProvider, $urlRouterProvider) {
            $stateProvider
                .state('address', {
                    url: '/address',
                    templateUrl: usercenter_templateprefix + 'address/address.html',
                    resolve: {
                        address_list: ['usercenter_userAddress', function (usercenter_userAddress) {
                            var alladdress = usercenter_userAddress.alladdress();
                            if (alladdress.$promise) {
                                return alladdress.$promise;
                            } else {
                                return false;
                            }
                        }]
                    },
                    controller: 'AddressController'
                })

                .state('newaddress', {
                    url: '/newaddress',
                    templateUrl: usercenter_templateprefix + 'address/newaddress.html',
                    controller: 'NewaddressController'
                })
                .state('reviseaddress', {
                    url: '/reviseaddress/:contactid',
                    templateUrl: usercenter_templateprefix + 'address/reviseaddress.html',
                    controller: 'ReviseAddressController'
                })

        }])
    //收货地址页面控制
    .controller('AddressController', ['$scope', '$state', 'usercenter_userAddress', 'address_list', function ($scope, $state, usercenter_userAddress, address_list) {
        dismiss();
        //获取有的地址信息
        $scope.address_list = address_list;
        $scope.defaultAddressCss = function () {
            var address = document.getElementsByName('address');
            var defaultText = document.querySelectorAll('.consumer_condition')
            address[0].checked = true;
            defaultText[0].innerHTML = '默认地址'
        }
        //修改地址信息
        $scope.revise = function (address) {
            for (var i = 0; i < $scope.address_list.length; i++) {
                if ($scope.address_list[i].contactid == address.contactid) {
                    $state.go("reviseaddress", {contactid: address.contactid})
                }
            }
        }

        //删除地址
        $scope.myque_remove = function (contactid) {
            $(".contain-item").fadeIn();
            $scope.remove = function () {
                //这个是地列表，这是一个数组所有要遍历找到id节点
                var indextodelete = -1;
                for (var i = 0; i < $scope.address_list.length; i++) {
                    if ($scope.address_list[i].contactid == contactid) {
                        //从数组中删除这个id的�??
                        indextodelete = i;
                        break;
                    }
                }
                if (indextodelete >= 0) {
                    $scope.address_list.splice(indextodelete, 1);
                }
                usercenter_userAddress.rmaddress(contactid).$promise.then(function (data) {
                    $(".contain-item").fadeOut();
                    input_alert("删除地址成功", 1000);
                }, function (data) {
                    input_alert("删除失败", 1000);
                })
            }
        }
        //设置默认地址
        $scope.updateDeafult = function (address) {
            var defaultText = $("#default" + address.contactid);
            if (defaultText.html() == '设置默认') {
                toastHtml('设置中', true);
                usercenter_userAddress.setDefault(address).$promise.then(function (data) {
                    //input_alert("设置成功");
                    var consumer_condition = $(".consumer_condition");
                    consumer_condition.html('设置默认');
                    defaultText.html('默认地址');
                    toastHtml('', false);
                }, function (error) {
                    input_alert("设置失败");
                });
            }
        }
    }])

    //新增地址页面
    .controller('NewaddressController', ['$scope', '$window','usercenter_userAddress', function ($scope,$window,  usercenter_userAddress) {
        $scope.provinceList = [];
        $scope.cityList = [];
        $scope.countyList = [];
        var district = {
            province: '',
            city: '',
            county: ''
        }
        var districtChoose = $("#districtChoose").find(".districtChoose");
        $scope.newadr_hide = function () {
            click_showadr(".newaddress_height_listbg", ".newaddress_height_list", "#hidval");
            if ($scope.provinceList.length == 0) {
                usercenter_userAddress.getProvince().$promise.then(function (data) {
                    $scope.provinceList = data;
                    districtChoose.hide();
                    districtChoose[0].style.display = 'block';
                })
            }
        }
        $scope.newaddress = {};
        $scope.save_address = function () {
            if (Object.keys($scope.newaddress).length == 0) {
                input_alert("请填写正确信息！");
            } else {
                $scope.newaddress.sex = $('input:radio:checked').val();
                usercenter_userAddress.add_address($scope.newaddress, function () {
                    input_alert('添加地址成功', 1000);
                    setTimeout(function () {
                        $window.history.back();
                    }, 1000);
                });
            }
        }
        usercenter_userAddress.getProvince().$promise.then(function (data) {
            $scope.provinceList = data;
            districtChoose.hide();
            districtChoose[0].style.display = 'block';
        })
        $scope.chooseProvince = function (province) {
            district.province = province.name;
            district.city = '';
            district.county = '';
            if (district.province == '台湾省' || district.province == '香港特别行政区' || district.province == '澳门特别行政区') {
                $("#district").html(district.province);
                $("#province").find('span')[0].innerHTML = province.name + "";
                $scope.newaddress.area = district.province;
                click_showadr(".newaddress_height_listbg", ".newaddress_height_list", "#hidval");
            } else {
                var name = district.province + district.city + district.county;
                $("#province").find('span')[0].innerHTML = province.name + "";
                usercenter_userAddress.getCity(province.provinceid).$promise.then(function (data) {
                    $scope.cityList = data;
                    $("#chooseAddr").html('<div id="province" class="choose" onclick="chooseDistrict(this,1)"> <span>' + district.province + '</span> </div><div id="city" class="choose active" onclick="chooseDistrict(this,2)"><span>请选择</span></div>');
                    districtChoose.hide();
                    districtChoose[1].style.display = 'block';
                })
            }
        }
        $scope.chooseCity = function (city) {
            district.city = city.name;
            district.county = '';
            var name = district.province + district.city + district.county;
            $("#city").find('span')[0].innerHTML = city.name + "";
            usercenter_userAddress.getCounty(city.cityid).$promise.then(function (data) {
                $scope.countyList = data;
                $("#chooseAddr").html('<div id="province" class="choose" onclick="chooseDistrict(this,1)"> <span>' + district.province + '</span> </div><div id="city" class="choose" onclick="chooseDistrict(this,2)"><span>' + district.city + '</span></div><div id="county" class="choose active" onclick="chooseDistrict(this,3)"><span>请选择</span></div>');
            })
            districtChoose.hide();
            districtChoose[2].style.display = 'block';
        }
        $scope.chooseCounty = function (county) {
            district.county = county.name;
            var name = district.province + district.city + district.county;
            $("#district").html(name);
            $("#county").find('span')[0].innerHTML = county.name + "";
            click_showadr(".newaddress_height_listbg", ".newaddress_height_list", "#hidval");
            $scope.newaddress.area = name;
        }
    }])
    //修改收货页面地址
    .controller('ReviseAddressController', ['$scope', '$state', '$stateParams', '$window', 'usercenter_userAddress', function ($scope, $state, $stateParams, $window, usercenter_userAddress) {
        $scope.provinceList = [];
        $scope.cityList = [];
        $scope.countyList = [];
        var district = {
            province: '',
            city: '',
            county: ''
        }
        var districtChoose = $("#districtChoose").find(".districtChoose");
        $scope.newadr_hide = function () {
            click_showadr(".newaddress_height_listbg", ".newaddress_height_list", "#hidval");
            if ($scope.provinceList.length == 0) {
                usercenter_userAddress.getProvince().$promise.then(function (data) {
                    $scope.provinceList = data;
                    districtChoose.hide();
                    districtChoose[0].style.display = 'block';
                })
            }
        }
        $scope.contactid = $stateParams.contactid;
        usercenter_userAddress.get_addressbyid($scope.contactid).$promise.then(function (data) {
            $scope.address = data;
            if (data.sex == 1) {
                $('#male').attr("checked", "checked")
            }
            else if (data.sex == 2) {
                $('#female').attr("checked", "checked")
            }
        });
        $scope.revise_address = function () {
            $scope.address.sex = $('input:radio:checked').val();
            usercenter_userAddress.updateaddress($scope.address).$promise.then(function (data) {
                input_alert("修改地址成功", 1000);
                setTimeout(function () {
                    $window.history.back();
                }, 1000)
            }, function (data) {
                input_alert("修改失败", 1000);
            })
        }
        usercenter_userAddress.getProvince().$promise.then(function (data) {
            $scope.provinceList = data;
            districtChoose.hide();
            districtChoose[0].style.display = 'block';
        })
        $scope.chooseProvince = function (province) {
            district.province = province.name;
            district.city = '';
            district.county = '';
            if (district.province == '台湾省' || district.province == '香港特别行政区' || district.province == '澳门特别行政区') {
                $("#district").html(district.province);
                $("#province").find('span')[0].innerHTML = province.name + "";
                $scope.address.area = district.province;
                click_showadr(".newaddress_height_listbg", ".newaddress_height_list", "#hidval");
            } else {
                var name = district.province + district.city + district.county;
                $("#province").find('span')[0].innerHTML = province.name + "";
                usercenter_userAddress.getCity(province.provinceid).$promise.then(function (data) {
                    $scope.cityList = data;
                    $("#chooseAddr").html('<div id="province" class="choose" onclick="chooseDistrict(this,1)"> <span>' + district.province + '</span> </div><div id="city" class="choose active" onclick="chooseDistrict(this,2)"><span>请选择</span></div>');
                    districtChoose.hide();
                    districtChoose[1].style.display = 'block';
                })
            }
        }
        $scope.chooseCity = function (city) {
            district.city = city.name;
            district.county = '';
            var name = district.province + district.city + district.county;
            $("#city").find('span')[0].innerHTML = city.name + "";
            usercenter_userAddress.getCounty(city.cityid).$promise.then(function (data) {
                $scope.countyList = data;
                $("#chooseAddr").html('<div id="province" class="choose" onclick="chooseDistrict(this,1)"> <span>' + district.province + '</span> </div><div id="city" class="choose" onclick="chooseDistrict(this,2)"><span>' + district.city + '</span></div><div id="county" class="choose active" onclick="chooseDistrict(this,3)"><span>请选择</span></div>');
            })
            districtChoose.hide();
            districtChoose[2].style.display = 'block';
        }
        $scope.chooseCounty = function (county) {
            district.county = county.name;
            var name = district.province + district.city + district.county;
            $("#district").html(name);
            $("#county").find('span')[0].innerHTML = county.name + "";
            click_showadr(".newaddress_height_listbg", ".newaddress_height_list", "#hidval");
            $scope.address.area = name;
        }
    }])